/* eslint-disable */
import Vue from 'vue'
import {setStorge, getStorge} from '@/util/storge'

export const state = {
  // classInfo
  classInfo: '',

  // 公共标题
  title: {
    tit: 'test',
    // 预留给返回键的回调函数，动态组件可能需要与主架交互
    cb: null
  },

  // 公共websocket
  ws: {
    wsObj: null,
    // 区分正在进行中的课程
    selected: -1
  },

  // 公共组件群
  components: {},
  // 冲突模块列表moduleConflictList
  mCL: {},

  // 倒计时timer
  timer: {}
}

Object.defineProperty(state, 'classInfo', {
  configurable: false,
  enumerable: true,
  get () {
    return getStorge()
  },
  set (newV) {
    setStorge(newV)
  }
})

export const action = {
  // 设置token
  setClassInfo (v) {
    if (store.debug) {
      console.log(v)
    }
    store.state.classInfo = v
  },

  // 清除token
  clearClassInfo () {
    if (store.debug) {
      console.log('清除token')
    }
    store.state.classInfo = {}
  },

  // 设置公共标题
  setTitAction (v) {
    if (store.debug) {
      console.log(v)
    }
    store.state.title.tit = v
  },

  // 设置websocket
  setWS (payload, cb) {
    if (store.debug) {
      console.log('设置websocket')
    }
    store.state.ws = payload

    if (cb && typeof cb === 'function') {
      cb()
    }
  },
  // 清除websocket
  clearWS () {
    if (store.debug) {
      console.log('清除websocket')
    }
    store.state.ws = {
      wsObj: null,
      selected: -1
    }
  },

  // 倒计时
  // cb：回调函数
  // comName：组件名
  // time：倒计时时间
  timeout (comName, time = 0, cb = () => {}) {
    state.timer[comName] = setTimeout(() => {
      if (typeof cb === 'function') {
        cb()
      }
      state.timer[comName] = undefined
    }, time)
  }
}

const store = {
  install (Vue) {
    Vue.prototype.$store = store
    Vue.prototype.$state = state
    Vue.prototype.$action = action
  },

  debug: false,

  state,

  action
}

Vue.use(store)

export default store
